package com.neusoft.bigdata.hbase.biz;

import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * @author lihzh
 * @date 2012-11-21 下午5:03:13
 */
public abstract class AbstractHBaseBiz {

	protected static Configuration conf;

	public static final String TABLE_NAME_STATISTIC = "statistic";
	public static final String TABLE_NAME_ORDER = "order";
	public static final String TABLE_NAME_METRIC = "metric";

	public static final String FAMILY_NAME_STINFO = "stinfo";
	public static final String FAMILY_NAME_BASEINFO = "baseinfo";
	public static final String FAMILY_NAME_ORINFO = "orinfo";
	public static final String FAMILY_NAME_METRICS = "metrics";

	public static final String COLUMN_NAME_RESOURCEID = "resourceid";
	public static final String COLUMN_NAME_PERIOD_TYPE = "periodtype";
	public static final String COLUMN_NAME_STATISTIC_TYPE = "statistictype";
	public static final String COLUMN_NAME_STARTTIME = "starttime";

	public static final String COLUMN_NAME_CPULOAD = "cpuload";
	public static final String COLUMN_NAME_MEMORYUSAGE = "memoryusage";
	public static final String COLUMN_NAME_MEMORYUSED = "memoryused";
	public static final String COLUMN_NAME_MEMORYTOTAL = "memorytotal";
	public static final String COLUMN_NAME_DISKFREE = "diskfree";
	public static final String COLUMN_NAME_DISKSIZE = "disksize";
	public static final String COLUMN_NAME_DISKIOREAD = "diskioread";
	public static final String COLUMN_NAME_DISKIOWRITE = "diskiowrite";
	public static final String COLUMN_NAME_NETIOWRITE = "netiowrite";
	public static final String COLUMN_NAME_NETIOREAD = "netioread";
	public static final String COLUMN_NAME_RECORDTIME = "recourdtime";

	public static final long STARTTIME = getTime(2011, 10, 1, 0);
	public static final long ENDTIME = getTime(2012, 11, 1, 0);
	public static final long INTERVAL = 5 * 60 * 1000L;

	static {
		conf = HBaseConfiguration.create();
		conf.addResource("hbase-stie.xml");
	}

	private static long getTime(int year, int month, int day, int hour) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day, hour, 0);
		return calendar.getTime().getTime();
	}

	protected Iterator<Result> getByFQN(HTable hTable, String startRowKey,
			String endRowKey) throws IOException {
		Scan scan = new Scan();
		scan.setStartRow(Bytes.toBytes(startRowKey));
		scan.setStopRow(Bytes.toBytes(endRowKey));
		long s = System.currentTimeMillis();
		ResultScanner scanner = hTable.getScanner(scan);
		long e = System.currentTimeMillis();
		System.out.println(e - s);
		return scanner.iterator();
	}

}
